<?php
/*
 * @分销商城
 * @功能说明：系统脚本服务
 * @更新说明：暂无更新
 * @文件名 RunController.class.php
 * @编码 UTF-8
 * @创建时间 2016-1-29 下午1:49:58
 * @创建人 XING
 */

namespace api\Controller;
class RunController extends ApibaseController
{
	/*
	 * 分销商城_服务订阅
	 * @date: 2016-1-29 下午1:49:52
	 * @editor: XING
	 */
	public function putService(){
		$getChannel = M('OrderEntity')->field('order_id,remark,cust_id')
					->where(array('order_status'=>'2','otype'=>'1'))->where('pay_at > 0 AND remark != ""')->select();
		if($getChannel){
			foreach($getChannel AS $val){
				$orderMain = M('OrderEntity');
				$orderMain -> startTrans();
				$putEffect = $orderMain->lock(true)
						   ->where(array('order_id'=>$val['order_id']))->setField(array(
								'order_status' => '9',
								'success_at' => time(),
								'update_at' => time()
							));
				if($putEffect){
					$markArr = explode('_',$val['remark']);
					switch($markArr[0]){
						//订阅讲故事
						case '2':
							$checkExist = M('SstoryEntity')
									    ->where(array('cust_id'=>$val['cust_id'],'clock_id'=>$markArr[1]))->find();
							
							$getMacCode = M('HwcodeEntity')->field('maccode')
										->where(array('cust_id'=>$val['cust_id']))->select();
							if($getMacCode){
								$storyMain = M('SstoryEntity');
								foreach($getMacCode AS $vmx){
									if($checkExist){
										$putIn = $storyMain->lock(true)->where('maccode = "'.$vmx['maccode'].'" AND cust_id = '.$val['cust_id'])->setField(array(
												'is_open' => '1',
												'end_at' => time()+30*24*3600,
												'update_at' => time()
										));
										if($putIn){
											$orderMain->commit();
											$storyMain->commit();
										}else{
											$orderMain->rollback();
											$storyMain->rollback();
										}
									}else{
										$putAdd = $storyMain->lock(true)->add(array(
												'maccode' => $vmx['maccode'],
												'cust_id' => $val['cust_id'],
												'is_open' => '1',
												'end_at' => time()+30*24*3600,
												'add_at' => time(),
												'update_at' => time()
										));
										if($putAdd){
											$orderMain->commit();
											$storyMain->commit();
										}else{
											$orderMain->rollback();
											$storyMain->rollback();
										}
									}
								}
							}
							break;
							
						//订阅闹钟
						case '3':
							$checkExist = M('SclockEntity')
										->where(array('cust_id'=>$val['cust_id'],'clock_id'=>$markArr[1]))->find();
							$getMacCode = M('HwcodeEntity')->field('maccode')
										->where(array('cust_id'=>$val['cust_id']))->select();
							if($getMacCode){
								$sEntity = M('SclockEntity');
								foreach($getMacCode AS $vmx){
									if($checkExist){
										$putIn = $sEntity->lock(true)->where('clock_id = '.$markArr[1].' AND maccode = "'.$vmx['maccode'].'" AND cust_id = '.$val['cust_id'])->setField(array(
												'is_open' => '1',
												'end_at' => time()+30*24*3600,
												'update_at' => time()
										));
										if($putIn){
											$sDescEntity = M('SclockDesc');
											$putDesc = $sDescEntity->lock(true)->where('clock_id = '.$markArr[1].' AND maccode = "'.$vmx['maccode'].'" AND cust_id = '.$val['cust_id'])->setField(array(
													'end_at' => time()+30*24*3600,
													'update_at' => time()
												));
											$sDescEntity->commit();
											$orderMain->commit();
											$sEntity->commit();
										}else{
											$orderMain->rollback();
											$sEntity->rollback();
										}
									}else{
										$putIn = $sEntity->lock(true)->add(array(
												'clock_id' => $markArr[1],
												'maccode' => $vmx['maccode'],
												'cust_id' => $val['cust_id'],
												'is_open' => '1',
												'end_at' => time()+30*24*3600,
												'add_at' => time(),
												'update_at' => time()
											));
										if($putIn){
											$orderMain->commit();
											$sEntity->commit();
										}else{
											$orderMain->rollback();
											$sEntity->rollback();
										}
									}
								}
							}
							
							break;
					}
				}else{
					$orderMain->rollback();
				}
			}
		}else{
			
		}
	}
	
	/*临时的机器入库*/
	public function putMac(){
		if(I('get.mac') && I('get.bdcode')){
			$is_exist = M('HwcodeEntity')
					  ->where('maccode = "'.trim(I('get.mac')).'" OR bdcode = "'.trim(I('get.bdcode')).'"')->count();
			if($is_exist){
				echo 'params wrong!';exit;
			}
			M('HwcodeEntity')->add(array(
					'maccode' => trim(I('get.mac')),
					'bdcode' => trim(I('get.bdcode'))
				));
			
			echo 'ok!';exit;
		}else{
			dataFormat('','0','fail');
		}
	}
}